package com.yh.autobio.gddb.gbmanager;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.yh.autobio.AppContext;
import com.yh.autobio.gddb.gdbean.BackMatBean;
import com.yh.autobio.gddb.gdbean.BackMatBeanDao;
import com.yh.autobio.gddb.gdbean.DaoMaster;
import com.yh.autobio.gddb.gdbean.DaoSession;

import java.util.ArrayList;
import java.util.List;

/**
 * =========================
 * 版权所有，违法必究
 * 作者：john
 * <p>
 * 工程：FoodBarcode
 * <p>
 * 文件名：BackMatManager
 * <p>
 * 创建时间：2017.05.27.14:44
 * *****************************
 */
public class BackMatManager {

    private final static String dbName = "fb_db";
    private static BackMatManager mInstance;
    private DaoMaster.DevOpenHelper openHelper;
    private Context context;
    private final DaoSession mDaoSession;

    public BackMatManager(Context context) {
        this.context = context;
        openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        mDaoSession = daoMaster.newSession();
    }

    /**
     * 获取单例引用
     *
     * @param context
     * @return
     */
    public static BackMatManager getInstance(Context context) {
        if (mInstance == null) {
            synchronized (BackMatManager.class) {
                if (mInstance == null) {
                    mInstance = new BackMatManager(context);
                }
            }
        }
        return mInstance;
    }

    /**
     * 获取可读数据库
     */
    private SQLiteDatabase getReadableDatabase() {
        if (openHelper == null) {
            openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
        }
        SQLiteDatabase db = openHelper.getReadableDatabase();
        return db;
    }

    /**
     * 获取可写数据库
     */
    private SQLiteDatabase getWritableDatabase() {
        if (openHelper == null) {
            openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
        }
        SQLiteDatabase db = openHelper.getWritableDatabase();
        return db;
    }

    public ArrayList<BackMatBean> getAllBack(){
        String name = AppContext.getInstance().getCurrentUser().getName();
        ArrayList<BackMatBean> backMatBeen = (ArrayList<BackMatBean>) mDaoSession.getBackMatBeanDao().queryBuilder().where(BackMatBeanDao.Properties.UserName.eq(name)).list();
//        ArrayList<BackMatBean> backMatBeen = (ArrayList<BackMatBean>) mDaoSession.getBackMatBeanDao().loadAll();
        return backMatBeen;
    }

    //插入一条记录
    public boolean insertBackMat(BackMatBean bean){
        long l = mDaoSession.getBackMatBeanDao().insertOrReplace(bean);
        if (-1!=l){
            return true;
        }else {
            return false;
        }
    }


    //插入集合
    public void insertBackMats(ArrayList<BackMatBean> list){
//        mDaoSession.getBackMatBeanDao().insertInTx(list);
        mDaoSession.getBackMatBeanDao().insertOrReplaceInTx(list);
//        mDaoSession.getBackMatBeanDao().updateInTx(list);
    }

    //更新count
    public void updateItemCount(BackMatBean bean){
        mDaoSession.getBackMatBeanDao().update(bean);
    }

    public BackMatBean getTDCStockByID(String id){
        String name = AppContext.getInstance().getCurrentUser().getName();
        BackMatBeanDao backMatBeanDao = mDaoSession.getBackMatBeanDao();
        BackMatBean unique = backMatBeanDao.queryBuilder().where(BackMatBeanDao.Properties.Id.eq(id),BackMatBeanDao.Properties.UserName.eq(name)).unique();
        return unique;
    }

    //删除所有数据
    public void delAllMat(){
        String name = AppContext.getInstance().getCurrentUser().getName();
        BackMatBeanDao matBeanDao = mDaoSession.getBackMatBeanDao();
        List<BackMatBean> list = matBeanDao.queryBuilder().where(BackMatBeanDao.Properties.UserName.eq(name)).list();
        matBeanDao.deleteInTx(list);
    }

    public void delAllByHeadId(long headId){
        String name = AppContext.getInstance().getCurrentUser().getName();
        BackMatBeanDao matBeanDao = mDaoSession.getBackMatBeanDao();
        List<BackMatBean> list = matBeanDao.queryBuilder().where(BackMatBeanDao.Properties.UserName.eq(name), BackMatBeanDao.Properties.HeadId.eq(headId)).list();
        matBeanDao.deleteInTx(list);
    }

    public void delMatByIds(String id , long headId){
        String name = AppContext.getInstance().getCurrentUser().getName();
        BackMatBeanDao matBeanDao = mDaoSession.getBackMatBeanDao();
        List<BackMatBean> list = matBeanDao.queryBuilder().where(BackMatBeanDao.Properties.UserName.eq(name), BackMatBeanDao.Properties.Id.eq(id), BackMatBeanDao.Properties.HeadId.eq(headId)).list();
        matBeanDao.deleteInTx(list);
    }

    public boolean delMat(BackMatBean bean){
        BackMatBeanDao matBeanDao = mDaoSession.getBackMatBeanDao();
        try {
            matBeanDao.delete(bean);
        }catch (Exception e){
            return false;
        }
        return true;
    }

    public List<BackMatBean> textGetAll(){
        BackMatBeanDao matBeanDao = mDaoSession.getBackMatBeanDao();
        List<BackMatBean> backMatBeen = matBeanDao.loadAll();
        return backMatBeen;
    }

    public List<BackMatBean> getBackMatsByHeadId(long headId){
        String name = AppContext.getInstance().getCurrentUser().getName();
        BackMatBeanDao matBeanDao = mDaoSession.getBackMatBeanDao();
        List<BackMatBean> list = matBeanDao.queryBuilder().where(BackMatBeanDao.Properties.UserName.eq(name), BackMatBeanDao.Properties.HeadId.eq(headId)).list();
        return list;
    }


}
